﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FB.Videos.ucCommon;
using FB.Bussiness;
using FB.DataAccess;
using log4net;

namespace FB
{
    public partial class VideoPage : System.Web.UI.Page
    {
        private static readonly ILog log = LogManager.GetLogger("Video");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                log.Info("Hit page" + DateTime.Now);
            }
        }

        private void LoadData()
        {
            string VideoYoutubeID = Request.QueryString["v"];
            string keyword = Request.QueryString["s"];
            if (VideoYoutubeID != null)
            {
                LoadVideoPlayer(VideoYoutubeID);
                LoadRelatedVideo(VideoYoutubeID);
            }
            else if(keyword != null && keyword != string.Empty)
            {
                LoadSearchVideo(keyword);
            }
            else
            {
                LoadFeatureVideo();
            }
        }

        private void LoadSearchVideo(string keyword)
        {
            this.Title = "Kết quả tìm kiếm";
            VideoBO VBO = new VideoBO();
            ucVideoList UCSearchResultVideo = (ucVideoList)LoadControl("Videos/ucCommon/ucVideoList.ascx");
            UCSearchResultVideo.ID = "ucListVideoRelated";
            UCSearchResultVideo.NumberDisplay = 30;
            UCSearchResultVideo.TitleDisplay = "Kết quả tìm kiếm";
            UCSearchResultVideo.VideoList = VBO.SearchVideo(keyword, 30);
            Top2.Controls.Add(UCSearchResultVideo);
        }

        private void ListVideoInCategory(PlaceHolder plPosition, Guid cateID, int limitNo)
        {
            //List video by category
            ucVideoInCategory ucVideoCate = (ucVideoInCategory)LoadControl("Videos/ucCommon/ucVideoInCategory.ascx");
            ucVideoCate.ID = "ucUserCtrl1";
            ucVideoCate.CategoryID = cateID;
            ucVideoCate.Limit = limitNo;
            plPosition.Controls.Add(ucVideoCate);
        }

        private void LoadFeatureVideo()
        {
            this.Title = "Dành cho thiếu nhi";
            VideoCategoryBO CatBO = new VideoCategoryBO();
            List<VideoCategory> ListCate = CatBO.GetVideoCategoryLimit(10);
            Random rand = new Random();
            List<VideoCategory> RandomCateList = ListCate.OrderBy(c => rand.Next()).Select(c => c).ToList();
            for (int i = 0; i < RandomCateList.Count; i++)
            {
                ucVideoInCategory ucVideoCate = (ucVideoInCategory)LoadControl("Videos/ucCommon/ucVideoInCategory.ascx");
                ucVideoCate.ID = "cate" + i; ;
                ucVideoCate.CategoryID = RandomCateList[i].ID;
                ucVideoCate.Limit = 15;
                Top1.Controls.AddAt(i, ucVideoCate);
            }
        }


        private void LoadVideoPlayer(string videoYoutubeID)
        {
            VideoBO VBO = new VideoBO();
            Video VideoPlay = VBO.GetVideoByYouTubeID(videoYoutubeID);
            if (VideoPlay != null)
            {
                this.Title = VideoPlay.Title;
                ucPlayVideo ucVideoPlayer = (ucPlayVideo)LoadControl("Videos/ucCommon/ucPlayVideo.ascx");
                ucVideoPlayer.ID = "ucUserCtrl1";
                ucVideoPlayer.ObjectID = videoYoutubeID;
                Top1.Controls.Add(ucVideoPlayer);
            }
            else
            {
                Response.Redirect("~/Video.aspx");
            }
        }
        private void LoadRelatedVideo(string videoYoutubeID)
        {
            //TODO: Get related video
            VideoBO VBO = new VideoBO();
            ucVideoList UCRalatedVideo = (ucVideoList)LoadControl("Videos/ucCommon/ucVideoList.ascx");
            UCRalatedVideo.ID = "ucListVideoRelated";
            UCRalatedVideo.NumberDisplay = 30;
            UCRalatedVideo.TitleDisplay = "";
            UCRalatedVideo.VideoList = VBO.GetRelatedVideo(videoYoutubeID, 30);
            Top2.Controls.Add(UCRalatedVideo);
        }
    }
}